 @include "readcsv.awk"
 @include "dist.awk"
 @include "lib.awk"
 @include "tiles.awk"
 function nearest(f,logp,key,out,\
                  name,lo,hi,goals,d,w,has,\
                  k,r0,r1,want,got) {
   readcsv(name,lo,hi,goals,d,w,has,f,logp)
   for(r0 in d) {
     dots()
     r1      = closest(d,r0,w,lo,hi,goals)
     want    = theClass(goals,r1,d)
     got     = theClass(goals,r0,d)
     if (logp) { want = E^want; got = E^got }
     out[key][r0] = abs((want - got)/ want)
 }}
 function closest(d,r0,w,lo,hi,goals,  r,r1,tmp,min) {
   min = 10^10
   for(r in d)
     if (r != r0) {
       tmp = dist(r0,r,w,lo,hi,d,goals)
       if( tmp < min ) {
         r1  = r
         min = tmp  }}
   return r1
 }   

 
